package vn.edu.tdt.it.domain;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;

public class StaticConnection {
	static private Connection cn = null;
	static private Statement st = null;
	static private PreparedStatement pst = null;
	static String JDBC_DRIVER = "com.mysql.jdbc.Driver";  
	public static String DB_URL = "jdbc:mysql://localhost/wikidata?useUnicode=true&characterEncoding=UTF-8";
	static public Connection getConnection(){
		if(cn==null){
			try {
				Class.forName("com.mysql.jdbc.Driver");
				System.out.println("Connecting to database...");
			    cn = DriverManager.getConnection(DB_URL,"root","");
			    System.out.println("Creating statement...");
			    st = cn.createStatement();
			} catch (Exception e) {
				e.printStackTrace();
			}
		      
		}
		return cn;
	}
	
	static public Statement getStatement(){
		if(st==null){
			getConnection();
		}
		return st;
	}
	
	static public PreparedStatement getPreparedStatement(String sql){
		if(pst==null){
			getConnection();
		}
		try {
			return cn.prepareStatement(sql);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
}
